package com.comdo.newup_loan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.model.PresentRecordBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liyuan on 2017/9/27.
 */

public class PresentRecordAdapter extends RecyclerView.Adapter<PresentRecordAdapter.ViewHolder> implements View.OnClickListener {

    private Context mContext;
    private List<PresentRecordBean> mlist = new ArrayList<PresentRecordBean>();
    private OnItemClickListener mOnItemClickListener = null;


    public PresentRecordAdapter(ArrayList<PresentRecordBean> datas, Context context) {
        this.mlist = datas;
        this.mContext = context;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {

        viewHolder.itemView.setTag(position);
        viewHolder.tv_date.setText(mlist.get(position).getCreateTime());
        viewHolder.tv_amount.setText(mlist.get(position).getWithdrawMoney()+"元");
        if(mlist.get(position).getState()==-1||mlist.get(position).getState()==0){
            viewHolder.tv_state.setText("待处理");
        }else if(mlist.get(position).getState()==1){
            viewHolder.tv_state.setText("已处理");
        }else if(mlist.get(position).getState()==2){
            viewHolder.tv_state.setText("驳回");
        }
        if(mlist.get(position).getBankState()==0){
            viewHolder.tv_bank_state.setText("出款成功");
            viewHolder.tv_amount.setTextColor(mContext.getResources().getColor(R.color.green));
        }else if(mlist.get(position).getBankState()==1){
            viewHolder.tv_bank_state.setText("出款失败");
            viewHolder.tv_amount.setTextColor(mContext.getResources().getColor(R.color.red));
        }else if(mlist.get(position).getBankState()==2){
            viewHolder.tv_amount.setTextColor(mContext.getResources().getColor(R.color.red));
            viewHolder.tv_bank_state.setText("处理中");
        }else{
            viewHolder.tv_amount.setTextColor(mContext.getResources().getColor(R.color.red));
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_present_record, parent, false);
        ViewHolder vh = new ViewHolder(view);
        view.setOnClickListener(this);
        return vh;
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_date;
        TextView tv_amount;
        TextView tv_state;
        TextView tv_bank_state;

        public ViewHolder(View view) {
            super(view);
            tv_date = (TextView) view.findViewById(R.id.tv_date);
            tv_amount = (TextView) view.findViewById(R.id.tv_amount);
            tv_state = (TextView) view.findViewById(R.id.tv_state);
            tv_bank_state = (TextView) view.findViewById(R.id.tv_bank_state);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
